#include "Screen.h"

N2F::Iw3D::Screen::Screen(const ScaleFactor &scaleFactor)
	: scaleFactor(scaleFactor)
{
	this->position = CIwFVec2(0.0f, 0.0f);
	this->state = Null;

	return;
}

N2F::Iw3D::Screen::~Screen()
{
	return;
}

void N2F::Iw3D::Screen::init()
{
	this->OnInit();

	return;
}

void N2F::Iw3D::Screen::reset()
{
	this->OnReset();

	return;
}

void N2F::Iw3D::Screen::kill()
{
	if (!this->elements.empty())
	{
		for (ElementListIter it = this->elements.begin(); it != this->elements.end(); ++it)
		{
			(*it).second->kill();
			delete (*it).second;
		}

		this->elements.clear();
	}

	this->OnKill();

	return;
}

void N2F::Iw3D::Screen::beforeActive()
{
	this->OnBeforeActive();

	return;
}

void N2F::Iw3D::Screen::beforeSuspended()
{
	this->OnBeforeSuspended();

	return;
}

void N2F::Iw3D::Screen::beforeBackground()
{
	this->OnBeforeBackground();

	return;
}

void N2F::Iw3D::Screen::touch(N2F::Iw3D::InputPoint point)
{
	this->OnTouch(point);

	if (!this->touches.empty())
	{
		for (TouchAssocListIter tt = this->touches.begin(); tt != this->touches.end(); ++tt)
		{
			if ((*tt).second == (int)point.pointId)
			{
				if (this->elements[(*tt).first]->isHit(CIwFVec2((float)point.x, (float)point.y)))
				{
					this->elements[(*tt).first]->OnTouch(point);

					if (!point.isMoving && !point.isPressed)
					{
						this->touches.erase(tt);
					}

					return;
				}
				else
				{
					this->touches.erase(tt);

					break;
				}
			}
		}
	}

	if (point.isPressed)
	{
		for (ElementListIter it = this->elements.begin(); it != this->elements.end(); ++it)
		{
			if ((*it).second->isHit(CIwFVec2((float)point.x, (float)point.y)))
			{
				this->touches.insert(TouchAssocListItem((*it).first, point.pointId));
				(*it).second->OnTouch(point);

				break;
			}
		}
	}

	return;
}

void N2F::Iw3D::Screen::key(N2F::Iw3D::InputKey key)
{
	this->OnKey(key);

	return;
}

void N2F::Iw3D::Screen::draw(int64 delta)
{
	this->OnDraw(delta);

	return;
}

void N2F::Iw3D::Screen::font(int64 delta)
{
	this->OnFont(delta);

	return;
}

void N2F::Iw3D::Screen::update(int64 delta)
{
	this->OnUpdate(delta);

	return;
}

void N2F::Iw3D::Screen::OnTouch(InputPoint point)
{
	return;
}

void N2F::Iw3D::Screen::OnKey(InputKey key)
{
	for (ElementListIter it = this->elements.begin(); it != this->elements.end(); ++it)
	{
		(*it).second->OnKey(key);
	}

	return;
}

const CIwFVec2 N2F::Iw3D::Screen::getPosition() const
{
	return(this->position);
}

void N2F::Iw3D::Screen::setPosition(CIwFVec2 pos)
{
	this->position = pos;

	return;
}

void N2F::Iw3D::Screen::move(float x, float y)
{
	this->position.x += x;
	this->position.y += y;

	return;
}

void N2F::Iw3D::Screen::move(CIwFVec2 move)
{
	this->position += move;

	return;
}

const N2F::Iw3D::Screen::State N2F::Iw3D::Screen::getState() const
{
	return(this->state);
}

void N2F::Iw3D::Screen::setState(N2F::Iw3D::Screen::State state)
{
	this->state = state;

	return;
}
